package ren.steve.ngh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ren.steve.model.ngh.bean.NghQueryResult;
import ren.steve.ngh.mapper.NghQueryResultMapper;
import ren.steve.ngh.service.NghQueryResultService;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Author: CIS-T-13 Liu GuoJia
 * @Description:
 * @Date: Created in 2024/3/27 11:56
 */

@Service
public class NghQueryResultServiceImpl extends ServiceImpl<NghQueryResultMapper, NghQueryResult> implements NghQueryResultService {

  @Autowired
  NghQueryResultMapper nghQueryResultMapper;


  @Override
  public List<NghQueryResult> list(Integer tabId) {
    QueryWrapper<NghQueryResult> wrapper = new QueryWrapper<>();
    wrapper.eq("tab_id", tabId);
    return nghQueryResultMapper.selectList(wrapper);
  }

  @Override
  public NghQueryResult insert(NghQueryResult card, List<NghQueryResult> results) {
    List<Integer> nums = IntStream.rangeClosed(1, 999).boxed().collect(Collectors.toList());
    for (NghQueryResult result : results) {
      String[] split = result.getLabel().split("_");
      Integer num = Integer.valueOf(split[1]);
      int index = nums.indexOf(num);
      if (index >= 0) {
        nums.remove(index);
      }
    }
    card.setLabel("RESULT_" + nums.get(0).toString());;
    int result = nghQueryResultMapper.insert(card);
    if (result == 1) {
      return card;
    }
    return null;
  }

  @Override
  public NghQueryResult update(NghQueryResult card) {
    int result = nghQueryResultMapper.updateById(card);
    if (result == 1) {
      return card;
    }
    return null;
  }

  @Override
  public int removeById(String id) {
    return nghQueryResultMapper.deleteById(id);
  }

  @Override
  public NghQueryResult selectById(Integer id) {
    return nghQueryResultMapper.selectById(id);
  }
}
